package com.baicun.wish.controller;

import cn.hutool.json.JSONObject;
import com.baicun.wish.entity.ProvinceAdmit;
import com.baicun.wish.entity.dto.Result;
import com.baicun.wish.service.ProvinceAdmitService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baicun
 * @since 2020-09-07
 */
@RestController
@RequestMapping("/province-admit")
public class ProvinceAdmitController {

    @Autowired
    private ProvinceAdmitService provinceAdmitService;

    /**
     * 根据provinceConditionFrom获取所有省份的录取数据
     * create index index_province on province_admit(province);
     * @param jsonObject
     * @return
     */
    @PostMapping("/getProvinceAdmit")
    public Result getProvinceAdmit(@RequestBody JSONObject jsonObject){
        Integer currentPage = jsonObject.getInt("currentPage");
        JSONObject recommendConditionForm = jsonObject.getJSONObject("provinceConditionFrom");

        String province = recommendConditionForm.getStr("province");
        String examType = recommendConditionForm.getStr("examType");
        String year = recommendConditionForm.getStr("year");


        QueryWrapper<ProvinceAdmit> collegeQueryWrapper = new QueryWrapper<ProvinceAdmit>();
        Page page = new Page(currentPage,10);

        if (!"全部".equals(province)){
            collegeQueryWrapper = collegeQueryWrapper.eq("province",province);
        }
        if (!"全部".equals(examType)){
            collegeQueryWrapper = collegeQueryWrapper.eq("exam_type",examType);
        }
        if (!"全部".equals(year)){
            collegeQueryWrapper = collegeQueryWrapper.eq("year",year);
        }
        IPage<ProvinceAdmit> iPage = provinceAdmitService.page(page,collegeQueryWrapper.orderByDesc("province","year","score_line"));

        if (iPage.getRecords().isEmpty()){
            return Result.success(201,"查询列表为空",iPage);
        }else{
            return Result.success(iPage);
        }
    }

}
